package com.meixiang.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;

import com.meixiang.tool.Tool;

/**
 * author:xiejingwen
 */
public class SlideHorizontalView extends HorizontalScrollView{
    private View contentView;
    private View deleteView;
    private Context mContext;
    private boolean isShow=false;

    private int downScrollX;

    private int upScrollX;

    //移动的最小距离
    private int mTouchSlop;

    private IOpenOrClose mIOpenOrClose;

    public SlideHorizontalView(Context context) {
        this(context,null);
    }

    public SlideHorizontalView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public SlideHorizontalView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext=context;
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }

    @Override
    protected void onLayout(boolean changed, int l, final int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        LinearLayout linear= (LinearLayout) getChildAt(0);
        contentView=linear.getChildAt(0);
        deleteView=linear.getChildAt(1);
        ViewGroup.LayoutParams params=getLayoutParams();
        params.width= Tool.getWindowWith(mContext);
        contentView.setLayoutParams(params);
        contentView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isShow){
                    setShowView(false);
                }
            }
        });

    }

    public void setOpenOrCloseListener(IOpenOrClose mIOpenOrClose){
        this.mIOpenOrClose=mIOpenOrClose;
    }


    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()){
            case MotionEvent.ACTION_DOWN:
                downScrollX=getScrollX();
                break;
            case MotionEvent.ACTION_MOVE:
                break;
            case MotionEvent.ACTION_UP:
                upScrollX=getScrollX();
                int slideX=upScrollX-downScrollX;
                if (slideX <0&&Math.abs(slideX)>mTouchSlop&&isShow){
                    isShow = false;
                }else if (slideX >0&&Math.abs(slideX)>mTouchSlop&&!isShow){
                    isShow = true;
                }else{
                    if (upScrollX <=deleteView.getWidth() /2&&isShow) {
                        isShow = false;
                    }else if (upScrollX >deleteView.getWidth() /2&&!isShow){
                        isShow = true;
                    }
                }
                setShowView(isShow);
                super.onTouchEvent(ev);
                return  true;

        }
        return super.onTouchEvent(ev);
    }
    public void setShowView(boolean isShow){
        this.isShow=isShow;
        if (isShow){
            smoothScrollTo(deleteView.getWidth(),0);
        }else{
            smoothScrollTo(0,0);
        }
        if (mIOpenOrClose!=null){
            mIOpenOrClose.getStatus(isShow);
        }
    }
    public boolean isShowView(){
        return  isShow;
    }
    public interface IOpenOrClose{
        void getStatus(boolean isOpen);
    }
}
